<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="850px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">工序信息</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="procedureName">
              <el-input v-model.trim="ruleForm.procedureName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序编号" prop="procedureCode">
              <el-input v-model.trim="ruleForm.procedureCode" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属车间" prop="factoryId">
              <tree-select ref="treeSelect" style="width: 100%" :props="treeProps" :options="departData" :disabled="false" :accordion="true" :value="ruleForm.factoryId" search-key="factoryName" @getNode="getValue($event)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序状态" prop="status">
              <el-select style="width: 100%" v-model="ruleForm.status">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="停用" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="制品检验" prop="isCheck">
              <el-checkbox v-model="ruleForm.isCheck" :true-label="1" :false-label="0"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制品入库" prop="isStock">
              <el-checkbox v-model="ruleForm.isStock" :true-label="1" :false-label="0"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="外协工序" prop="isOutProcedure">
              <el-checkbox v-model="ruleForm.isOutProcedure" :true-label="1" :false-label="0"></el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  props: {},
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/pro/procedure/verifyNameUnique?procedureName=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('工序名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        procedureName: '',
        procedureCode: '',
        isStock: 0,
        isCheck: 0,
        isOutProcedure: 0,
        factoryId: '',
        status: ''
      },
      treeProps: {
        value: 'id',
        label: 'factoryName',
        children: 'children'
      },
      rules: {
        procedureName: [
          { required: true, message: '工序名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        procedureCode: [
          { required: true, message: '工序编号不能为空！', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      await this.getDepartData()
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取所有车间
     * @return {*}
     */
    async getDepartData() {
      const res = await this.$axios.get('/pro/factory/list')
      this.departData = [{ id: '0', factoryName: '无' }, ...res.data.data]
    },
    getValue(val) {
      this.ruleForm.factoryId = val.id
      this.ruleForm.factoryName = val.factoryName
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/pro/procedure/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        procedureName: '',
        procedureCode: '',
        isStock: 0,
        isCheck: 0,
        isOutProcedure: 0,
        factoryId: '',
        status: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
